package com.changgou.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.content.feign.ContentFeign;
import com.changgou.content.pojo.Content;
import com.changgou.mq.message.TopicMessageSender;
import com.xpand.starter.canal.annotation.*;
import entity.Message;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * @author ：hushengjin
 */
@CanalEventListener
public class CanalDataEventListener {

    @Autowired
    private ContentFeign contentFeign;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private TopicMessageSender topicMessageSender;


    /**
     * 增加数据监听
     *
     * @param eventType
     * @param rowData
     */
    @InsertListenPoint
    public void onEventInsert(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList()
                .forEach((c) -> System.out.println("By--增加后的数据: "
                        + c.getName() + " ::   " + c.getValue()));
    }

    /**
     * 修改数据监听
     *
     * @param eventType
     * @param rowData
     */
    @UpdateListenPoint
    public void onEventUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList()
                .forEach((c) -> System.out.println("By--修改后的数据: "
                        + c.getName() + " ::   " + c.getValue()));
    }

    /**
     * 删除数据监听
     *
     * @param eventType
     */
    @DeleteListenPoint
    public void onEventDelete(CanalEntry.EventType eventType) {
        System.out.println("DeleteListenPoint");
    }

    /**
     * 自定义数据修改监听
     *
     * @param eventType
     * @param rowData
     */
    @ListenPoint(
            destination = "example",
            schema = "changgou_content",
            table = {"tb_content", "tb_content_category"},
            eventType = CanalEntry.EventType.UPDATE
    )
    public void onEventCustomUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //获取广告分类ID
        String categoryId = getColumn(rowData, "category_id");
        //根据广告分类ID查询所有广告
        Result<List<Content>> result = contentFeign.findByCategory(Long.valueOf(categoryId));
        List<Content> contentList = result.getData();
        //将广告数据存入redis缓存
        //int i = 1/0;
        stringRedisTemplate.boundValueOps("content_" + categoryId).set(JSON.toJSONString(contentList));
    }

    @ListenPoint(
            destination = "example",
            schema = "changgou_goods",
            table = {"tb_spu"},
            eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.DELETE}
    )
    public void onEventCustomSpu(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //获取操作类型
        int code = eventType.getNumber();
        //获取操作的数据,主键
        String id = getColumn(rowData, "id");
        //封装message
        Message message = new Message(code, id, "topic_queue_spu", "topic_exchange_spu");
        //发送消息
        topicMessageSender.sendMessage(message);
    }

    /**
     * 获取指定列的值
     *
     * @param rowData
     * @param columnName
     * @return
     */
    private String getColumn(CanalEntry.RowData rowData, String columnName) {
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if (column.getName().equalsIgnoreCase(columnName)) {
                return column.getValue();
            }
        }

        //可能是删除操作
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            if (column.getName().equalsIgnoreCase(columnName)) {
                return column.getValue();
            }
        }
        return null;
    }


}
